<?php
namespace Admin\Controller;
use Think\Controller;
class LoginController extends Controller {
    public function index(){
        $this->display();
    }
    //登录验证码
    public function verify(){
        $Verify = new \Think\Verify();
        $Verify->fontSize = 30;
        $Verify->length = 5;
        $Verify->useNoise = false;
        $Verify->entry();
    }

    public function login(){

        if (!IS_POST)  halt('页面不存在');

        $pUsername = I('username','','trim');
        $pPassword = I('password','');
        $pVerify = I('code','','trim');
                           
        if (!check_verify($pVerify)) {
            $this->error('验证码不正确');
        }


        if ($pUsername == '' || $pPassword == '') {
            $this->error('账号或密码不能为空');
        }

        $aRow = M('admin')->where(array('username' => $pUsername))->find();

        if (!$aRow || ($aRow['password'] != get_password($pPassword, $aRow['encrypt']))) {
            $this->error('账号或密码错误');
        }

        //更新数据库的参数
        $tData = array('id' => $aRow['id'] ,//保存时会自动为此ID的更新
            'logintime' => time(),
            'loginip' => get_client_ip()
        );
        //更新数据库
        M('admin')->save($tData);

        //保存Session
        session('ru_adm_userid', $aRow['id']);
        session('ru_adm_username', $aRow['username']);
        session('ru_adm_logintime', date('Y-m-d H:i:s', time()));
        session('ru_adm_loginip', get_client_ip());

        //跳转
        $this->redirect(MODULE_NAME.'/Index/index');
    }
    //退出
    public function logout() {
        session_unset();
        session_destroy();
        $this->redirect(MODULE_NAME.'/Login/index');
    }
}
